﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OptionsForm.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G. Vacirca
// </author>
// <summary>
//   Defines the OptionsForm type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.UI.Forms
{
    using System;
    using System.Windows.Forms;
    
    using Application;
    using Data;

    /// <summary>
    /// The Options window.
    /// </summary>
    public partial class OptionsForm : InfoBaseForm
    {
        #region "Constructors"

        /// <summary>
        /// Initializes a new instance of the <see cref="OptionsForm"/> class.
        /// </summary>
        public OptionsForm()
        {
            InitializeComponent();
        }

        #endregion

        #region "Public Members"

        /// <summary>
        /// Gets WindowTittle.
        /// </summary>
        public static string WindowTittle
        {
            get
            {
                return "Options";
            }
        }

        #endregion        

        #region "UI Events"

        /// <summary>
        /// Form load event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event parameters.</param>
        private void OptionsForm_Load(object sender, EventArgs e)
        {
            SettingsPropertyGrid.SelectedObject = ApplicationConfiguration.Settings;
        }

        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event parameters.</param>
        private void SaveButton_Click(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show("Are you sure you want to save the changes?", "Save changes...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            if (dialogResult.Equals(DialogResult.Yes))
            {
                try
                {
                    var settings = (AppSettingsData)SettingsPropertyGrid.SelectedObject;
                    ApplicationConfiguration.Settings = settings;
                    ApplicationConfiguration.SaveSettings();

                    MessageBox.Show("Changes were sucessfully applied!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("There was an error while trying to save the changes.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }                               
            }
        }

        /// <summary>
        /// Cancel the changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event parameters.</param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show("Are you sure you want to cancel the changes?", "Cancel changes...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            if (dialogResult.Equals(DialogResult.Yes))
            {
                MessageBox.Show("Changes were canceled!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
        }

        /// <summary>
        /// Property changed event.
        /// </summary>
        /// <param name="s">The s object.</param>
        /// <param name="e">The event parameters.</param>
        private void SettingsPropertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            SaveButton.Enabled = true;
            CancelButton.Enabled = true;
        }

        #endregion
    }
}
